<template>
  <BasicTable @register="registerTable" :rowSelection="rowSelection">
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
</template>

<script lang="ts" name="online-user" setup>
  import { BasicTable, TableAction } from '/@/components/Table';
  import { columns, searchFormSchema } from './OnlineUser.data';
  import { list, forceLogout } from './OnlineUser.api';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useI18n } from '/@/hooks/web/useI18n';
  const { t } = useI18n('monitor.onlineuser');
  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: 'online-user',
    tableProps: {
      title: t('title'),
      api: list,
      columns: columns,
      formConfig: {
        schemas: searchFormSchema,
      },
      actionColumn: {
        width: 120,
      },
    },
  });
  const [registerTable, { reload }, { rowSelection }] = tableContext;
  const $message = useMessage();

  //操作栏
  function getTableAction(record) {
    return [
      {
        label: t('forceLogout'),
        popConfirm: {
          title: t('forceLogoutTip1'),
          confirm: handleForce.bind(null, record),
        },
      },
    ];
  }

  /**
   * 强退
   * @param record
   */
  function handleForce(record) {
    forceLogout({ token: record.token }).then((res) => {
      if (res.success) {
        reload();
        $message.createMessage.success(t('forceLogoutTip2', { name: record.realname }));
      } else {
        $message.createMessage.warn(res.message);
      }
    });
  }
</script>

<style scoped></style>
